package com.ruoyi.web.controller.prediction;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.prediction.dto.PredictionDto;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.prediction.domain.Prediction;
import com.ruoyi.prediction.service.IPredictionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预测数据Controller
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/control/prediction")
public class PredictionController extends BaseController
{
    @Autowired
    private IPredictionService predictionService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    @Scheduled(cron = "0 0 0/2 * * ?")
//    @Scheduled(cron = "0/3 * * * * ?")
    public void insertPrediction(){
        predictionService.addPrediction();
    }

    @GetMapping("/getData/{sensorType}")
    public PredictionDto getData(@PathVariable("sensorType") String sensorType){
        String type = sysDictDataService.selectDictLabel("sensor_type", sensorType);
        PredictionDto predictionDto = predictionService.getData(type);
        return predictionDto;
    }

    /**
     * 查询预测数据列表
     */
    @PreAuthorize("@ss.hasPermi('control:prediction:list')")
    @GetMapping("/list")
    public TableDataInfo list(Prediction prediction)
    {
        startPage();
        List<Prediction> list = predictionService.selectPredictionList(prediction);
        return getDataTable(list);
    }

    /**
     * 导出预测数据列表
     */
    @PreAuthorize("@ss.hasPermi('control:prediction:export')")
    @Log(title = "预测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prediction prediction)
    {
        List<Prediction> list = predictionService.selectPredictionList(prediction);
        ExcelUtil<Prediction> util = new ExcelUtil<Prediction>(Prediction.class);
        util.exportExcel(response, list, "预测数据数据");
    }

    /**
     * 获取预测数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:prediction:query')")
    @GetMapping(value = "/{predictionId}")
    public AjaxResult getInfo(@PathVariable("predictionId") String predictionId)
    {
        return success(predictionService.selectPredictionByPredictionId(predictionId));
    }

    /**
     * 新增预测数据
     */
    @PreAuthorize("@ss.hasPermi('control:prediction:add')")
    @Log(title = "预测数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prediction prediction)
    {
        return toAjax(predictionService.insertPrediction(prediction));
    }

    /**
     * 修改预测数据
     */
    @PreAuthorize("@ss.hasPermi('control:prediction:edit')")
    @Log(title = "预测数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prediction prediction)
    {
        return toAjax(predictionService.updatePrediction(prediction));
    }

    /**
     * 删除预测数据
     */
    @PreAuthorize("@ss.hasPermi('control:prediction:remove')")
    @Log(title = "预测数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{predictionIds}")
    public AjaxResult remove(@PathVariable String[] predictionIds)
    {
        return toAjax(predictionService.deletePredictionByPredictionIds(predictionIds));
    }
}
